package com.a01_2021.thousandstimes.instance.service.impl;

import com.a01_2021.thousandstimes.exception.CustomException;
import com.a01_2021.thousandstimes.exception.CustomExceptionType;
import com.a01_2021.thousandstimes.general.service.SysUserService;
import com.a01_2021.thousandstimes.instance.entity.WorkLog;
import com.a01_2021.thousandstimes.instance.mapper.WorkLogMapper;
import com.a01_2021.thousandstimes.instance.service.IWorkLogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author E1xP
 * @since 2021-03-10
 */
@Service
public class WorkLogServiceImpl extends ServiceImpl<WorkLogMapper, WorkLog> implements IWorkLogService {

    @Resource
    SysUserService sysUserService;

    @Resource
    WorkLogMapper workLogMapper;

    /**
     * 通过用户id获取其全部工作任务
     *
     * @param userId 用户id
     * @return List<WorkLog> 任务日志列表
     * @throws CustomException 当不存在该用户是抛出该异常
     */
    @Override
    public List<WorkLog> getAllWorkLog(long userId) throws CustomException {
        if (!sysUserService.isUser(userId)) {
            //请求用户不存在
            throw new CustomException(CustomExceptionType.USER_NOT_FOUND_ERROR, "所请求的用户不存在");
        }
        QueryWrapper<WorkLog> workLogQueryWrapper = new QueryWrapper<>();
        workLogQueryWrapper.eq("user_id", userId);
        return workLogMapper.selectList(workLogQueryWrapper);
    }

    /**
     * 添加工作任务
     *
     * @param userId      用户id
     * @param workPlan    工作计划
     * @param workRecord  工作记录
     * @param workSummary 工作总结
     * @return WorkLog 工作任务实体类
     */
    @Override
    public WorkLog addWorkLog(long userId, String workPlan, String workRecord, String workSummary) {
        //设置信息
        WorkLog workLog = new WorkLog(null, userId, false, workPlan, workRecord, workSummary, LocalDateTime.now());
        //向数据库添加数据
        workLogMapper.insert(workLog);
        return workLog;
    }

    /**
     * 通过工作任务id查询是否为现有工作任务
     *
     * @param id 工作任务id
     * @return Boolean 是否为现有工作任务
     */
    public Boolean isWorkLog(long id) {
        QueryWrapper<WorkLog> workLogQueryWrapper = new QueryWrapper<>();
        workLogQueryWrapper.eq("id", id);
        int count = workLogMapper.selectCount(workLogQueryWrapper);
        return count != 0;
    }

    /**
     * 更新工作任务
     *
     * @param id          工作任务id
     * @param userId      用户id
     * @param workPlan    工作计划（可选
     * @param workRecord  工作记录（可选
     * @param workSummary 工作总结（可选
     * @return WorkLog 工作任务实体列类
     * @throws CustomException 当 工作任务不属于请求用户,工作任务不存在 时抛出该异常
     */
    public WorkLog updateWorkLog(long id, long userId, String workPlan, String workRecord, String workSummary) throws CustomException {
        if (!this.isWorkLog(id)) {
            //工作日志不存在
            throw new CustomException(CustomExceptionType.WORKLOG_NOT_FOUND_ERROR, "找不到该工作日志");
        }
        WorkLog workLog = workLogMapper.selectById(id);
        if (workLog.getUserId() != userId) {
            //该工作日志不属于该用户
            throw new CustomException(CustomExceptionType.OPERATION_NOT_ALLOW, "不允许修改他人工作任务");
        }
        //若有对应传入，则进行修改
        if (workPlan != null || workRecord != null || workSummary != null) {
            if (!StringUtils.isEmpty(workPlan)) {
                workLog.setWorkPlan(workPlan);
            }
            if (!StringUtils.isEmpty(workRecord)) {
                workLog.setWorkRecord(workRecord);
            }
            if (!StringUtils.isEmpty(workSummary)) {
                workLog.setWorkSummary(workSummary);
            }
            workLog.setDate(LocalDateTime.now());
            workLogMapper.updateById(workLog);
        }
        return workLog;
    }

    /**
     * 通过任务id完成任务
     *
     * @param id     任务id
     * @param userId 用户id
     * @return WorkLog实体类
     * @throws CustomException 当 用户不存在,工作日志不属于该用户 时抛出
     */
    @Override
    public WorkLog finishWorkLog(long id, long userId) throws CustomException {
        if (!this.isWorkLog(id)) {
            //工作日志不存在
            throw new CustomException(CustomExceptionType.WORKLOG_NOT_FOUND_ERROR, "找不到该工作日志");
        }
        WorkLog workLog = workLogMapper.selectById(id);
        if (workLog.getUserId() != userId) {
            //该工作日志不属于该用户
            throw new CustomException(CustomExceptionType.OPERATION_NOT_ALLOW, "不允许完成他人任务");
        }
        workLog.setComplete(true);
        workLog.setDate(LocalDateTime.now());
        workLogMapper.updateById(workLog);
        return workLog;
    }

    @Override
    public WorkLog addWorkLogToUser(long userId, String workPlan, String workRecord, String workSummary) throws CustomException {
        if (!sysUserService.isUser(userId)) {
            //用户不存在
            throw new CustomException(CustomExceptionType.USER_NOT_FOUND_ERROR, "找不到该用户");
        }
        //设置工作任务
        WorkLog workLog = new WorkLog(null, userId, false, workPlan, workRecord, workSummary, LocalDateTime.now());
        //添加到数据库
        workLogMapper.insert(workLog);
        return workLog;
    }
}
